package com.github.xuemingyu.springboot.acticitycloud.controller.api;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.bean.EntityToVo;
import com.bidanet.bdcms.core.common.EntityTool;
import com.bidanet.bdcms.core.vo.Page;
import com.github.xuemingyu.springboot.acticitycloud.config.DingConfig;
import com.github.xuemingyu.springboot.acticitycloud.entity.Activity;
import com.github.xuemingyu.springboot.acticitycloud.entity.enumEntity.ActivityStatus;
import com.github.xuemingyu.springboot.acticitycloud.entity.vo.ActivityVo;
import com.github.xuemingyu.springboot.acticitycloud.entity.vo.AuditVo;
import com.github.xuemingyu.springboot.acticitycloud.service.ActivityService;
import com.taobao.api.ApiException;
import kotlinx.html.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/activity")
public class ActivityApiController {
    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "/query")
    public ApiResult index(Activity activity, Page page, HttpServletRequest request) {
        try {
            activity.setUserId(request.getHeader("userId"));
            activityService.queryLike(activity, page);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success(page);
    }

    @RequestMapping(value = "pcQuery")
    public ApiResult pcQuery(Activity activity, Page page, HttpServletRequest request) {
        try {
            String userId = request.getHeader("userId");
            activityService.pcQuery(activity, page, userId);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return ApiResult.success(page);
    }

    @RequestMapping("/findOne")
    public ApiResult findOne(Long id, HttpServletRequest request) {
        Activity activity = activityService.findActivity(id);
        activity.setUserId(request.getHeader("userId"));
        EntityToVo entityToVo = new EntityTool().createEntityToVo(ActivityVo.class, activity);
        return ApiResult.success(entityToVo);
    }

    @RequestMapping("/add")
    public ApiResult insert(Activity activity, HttpServletRequest request) {
        activity.setUserId(request.getHeader("userId"));
        try {
            activityService.insert(activity);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("添加成功");
    }

    @RequestMapping("/modify")
    public ApiResult update(Activity activity, HttpServletRequest request) {
        activity.setUserId(request.getHeader("userId"));
        try {
            activityService.update(activity);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("修改成功");
    }

    @RequestMapping("/delete")
    public ApiResult delete(String ids) {
        try {
            activityService.delete(ids);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }

        return ApiResult.success("删除成功");
    }

    @RequestMapping("/approve")
    public ApiResult publishActivity(String ids) {
        try {
            activityService.publishActivity(ids);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("发布成功");
    }

    @RequestMapping("/cancel")
    public ApiResult cancel(Long id, String remark) {
        activityService.cancelActivity(id, remark);
        return ApiResult.success("取消发布成功");
    }

    @RequestMapping("/top")
    public ApiResult upActivity(String ids) {
        try {
            activityService.upActivity(ids);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("操作成功");
    }

    @RequestMapping("/myJoin")
    public ApiResult myJoin(ActivityStatus status, HttpServletRequest request, Page page) {
        String userId = request.getHeader("userId");

        activityService.myJion(userId, status, page);
        Page<EntityToVo> entityToVo = new EntityTool().getPageEntityToVo(ActivityVo.class, page);

        return ApiResult.success(entityToVo);
    }

    @RequestMapping("/updatePublish")
    public ApiResult updatePublish(Activity activity) {
        activityService.updatePublish(activity);
        return ApiResult.success("修改成功");
    }

    /**
     * 我创建的活动
     *
     * @param activity
     * @param page
     * @param request
     * @return
     */
    @RequestMapping("/myCreate")
    public ApiResult myCreate(Activity activity, Page<Activity> page, HttpServletRequest request) {
        activity.setUserId(request.getHeader("userId"));
        activityService.queryAppActivity(activity, page);
        return ApiResult.success(page);
    }

    /**
     * 撤销活动
     *
     * @param id
     * @param cancelRemark
     * @return
     */
    @RequestMapping("/cancelActicity")
    public ApiResult cancelActicity(Long id, String cancelRemark) {
        activityService.cancelActicity(id, cancelRemark);
        return ApiResult.success("撤销成功");
    }

    /**
     * app发布活动
     *
     * @param activity
     * @return
     */
    @RequestMapping("/addAppActivity")
    public ApiResult addAppActivity(Activity activity, HttpServletRequest request) {
        activity.setUserId(request.getHeader("userId"));
        activityService.addAppActivity(activity);
        return ApiResult.success("发布成功");
    }

    /**
     * pc发布常规活动
     *
     * @param activity
     * @return
     */
    @RequestMapping("addPCActivity")
    public ApiResult addPCActivity(Activity activity, HttpServletRequest request) {
        activity.setUserId(request.getHeader("userId"));
        activityService.addPCActivity(activity);
        return ApiResult.success("添加成功");
    }

    /**
     * 审批活动
     *
     * @param activity
     * @param request
     * @return
     * @throws ApiException
     */
    @RequestMapping("/auditActivity")
    public ApiResult auditActivity(Activity activity, HttpServletRequest request) throws ApiException {
        activity.setUserId(request.getHeader("userId"));
        activityService.auditActivity(activity);
        return ApiResult.success("审批完成");
    }

    /**
     * 查询待审批活动
     *
     * @param activity
     * @param page
     * @return
     */
    @RequestMapping("/queryWaitAudit")
    public ApiResult queryWaitAudit(Activity activity, Page<Activity> page) {
        activityService.queryWaitAudit(activity, page);
        Page<EntityToVo> pageq = new EntityTool().getPageEntityToVo(AuditVo.class, page);
        return ApiResult.success(pageq);
    }

    /**
     * 查询已审批活动
     *
     * @param activity
     * @param page
     * @return
     */
    @RequestMapping("/queryAlreadyAudit")
    public ApiResult queryAlreadyAudit(Activity activity, Page<Activity> page) {
        activityService.queryAlreadyAudit(activity, page);
        Page<EntityToVo> pageq = new EntityTool().getPageEntityToVo(AuditVo.class, page);
        return ApiResult.success(pageq);
    }
}
